import { observable, action,toJS } from 'mobx';
import {WeaTableNew,WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew;
import { ListStore } from './listStore';
import {Modal,message} from "antd"
import * as  Apis from '../apis/search';
import * as Common_Apis from '../apis/index'
import * as Capital_Apis from "../apis/capital"
import {ShareStore} from "./shareStore"
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

class SearchStore  extends ListStore{	
    @observable title = "查询资产";

    @observable conditiontype = {isdata:"2"};
    @observable rightMenu =[];
    @observable condition = [];
    @observable capitalid = "";
    @observable treeSearchParams = {};
    @observable isdata = "";
    @observable shareStore = new ShareStore();//共享
    @observable leftShow = false;
    @observable appDetachDisableAll =false;
    @observable locationparams = "";//门户搜索
    @observable
    showLeftTree = (bool) => {
        this.leftShow = bool;
    }

    @action
    initDatas=(params={})=>{
        this.loading = true;
        if(params.isdata){
            this.isdata = params.isdata;
        }
        if(this.isdata == "1"){
            this.title = getLabel(33182,"查询资产资料");
        }else{
            this.title = getLabel(16425,"查询资产");
        };
        const newParams = {...this.treeSearchParams,...this.conditiontype,...params};
        this.locationparams = params;
        if(this.form && !this.form.isFormInit){
            this.condition = [];
            this.form = new WeaForm();
            Apis.getSearchCondition(newParams).then(data=>{
                this.loading = false;
                this.condition = data.condition;
                this.conditiontype = {...this.conditiontype,...params};
                this.form.initFormFields(data.condition);
                if(this.form.has("name")){
                    this.form.updateFields({name:{value:params.name}},false);
                }else{
                    this.form.add('',{key:"name"});
                    this.form.updateFields({name:{value:params.name}},false);
                }
           })
        }
    }
    @action
    initTreeDatas =(params={}) =>{
        Apis.getSearchTree({...this.conditiontype,...params}).then(data=>{
			this.leftTree = data.treedata;
			this.leftTreeCount = data.treecount;
			this.leftTreeCountType = data.countcfg;
			this.selectedTreeKey = this.selectedTreeKey|| data.selectedKeys || "" ;
		})
     }
     @action
    doSearch=(params={},type)=>{
		this.loading = true;
        if(type == "tree"){
            this.treeSearchParams = {...this.treeSearchParams,...params};
            this.initDatas({...toJS(this.conditiontype),name:''});//清空name值，点击树形切换，清空form所有值，除了资产组
        }
        //获取表单的参数值
        const  searchParamsAd= this.form.getFormParams();
        let newParams = { ...this.conditiontype,
            ...this.treeSearchParams, ...searchParamsAd,...this.locationparams,...params};
        if(this.form && this.form.isFormInit){
                newParams = { ...this.conditiontype,
                ...this.treeSearchParams,...this.locationparams,...searchParamsAd,...params};
        }

        Apis.getSearchList(newParams).then(data=>{
            this.tableStore = new TableStore();
			this.tableStore.getDatas(data.sessionkey,  params.current || 1);
			this.dataKey = data.sessionkey;
            this.loading = false;
            this.appDetachDisableAll = data.appDetachDisableAll;
        })
    }

    initRightMenu = (params={}) =>{
        Apis.getSearchRightMenu({...this.conditiontype,...params}).then(data=>{
			this.rightMenu = data.rightMenus;
		})
    }

	onShowColumn=()=>{
		this.tableStore.setColSetVisible(true);
		this.tableStore.tableColSet(true)
    }
    setSelectedTreeKey=(key) =>{
        this.selectedTreeKey = key;
    }
    // 导出Excel
    cptExport=(params={})=>{
        const searchParamsAd = this.form.getFormParams();
        let newParams = { ...this.conditiontype, ...toJS(this.treeSearchParams),...searchParamsAd,...this.locationparams,...params };
        if(this.form && this.form.isFormInit){
            newParams = { ...this.conditiontype, ...toJS(this.treeSearchParams),...this.locationparams,...searchParamsAd,...params };
        }
        Common_Apis.cptImport(newParams).then(data=>{
            window.location="/weaver/weaver.file.ExcelOut";
        })
    }
    //保存单行资产id
    saveCapitalId=(id)=>{
        this.capitalid = id;
    }
     //归还
     onGiveback = (params={}) =>{
        Common_Apis.doCptManager(params).then(data=>{
            if(data.msg==''){
                message.success(getLabel(83560,'归还成功！'));
				this.doSearch();
			}else{
				Modal.error({
                    title:  getLabel(15172,'系统提示'),
					content: data.msg,
				});
			}
        }).catch(err=>message.error(err))
    }
    //共享设置
    addCptShare =(params={})=>{
        Capital_Apis.addCptShare({capitalid:this.capitalid,...params}).then(data=>{
            message.success(getLabel('385412',"添加共享成功！"));
        })
    }


	clearStatus=()=>{
        this.conditiontype = {isdata:"2"};
        this.treeSearchParams = {};
        this.showSearchAd = false;
        this.treeSearchParams = {};
        this.selectedTreeKey = "";
        this.condition= [];
        this.clearFormFields();
        this.tableStore = new TableStore();
    }

    resetForm = () =>{
        this.form = new WeaForm();
    }

}


export default SearchStore;